// pages/community/searchList.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navbar: ['书评', '话题', '问答', '写文'],
        currentTab: 0,
        list:[],
        pagenum:1,
        cutype:0,
        cufrom:0,
        keyword:'',
        id:0,
        from:4,
        datainfo:{},
        pingLunData:{isShowPingLun:0,value:''},
        isShowPingLun:1,
        pingLunContent:'',
        bookImage:['1','1','1'],
        pinglunlists:[],
        liulannum:0
    },

    onReachBottom: function (e) {
        // var that = this;
        // // 显示加载图标
        // wx.showLoading({
        //     title: '玩命加载中',
        // })
        // that.setData({
        //     pageNum:1
        // })
        // this.getDataList();
    },
    onPullDownRefresh:function(e){
        var that = this;
        // console.log('下拉刷新')
        // var that = this;
        // that.setData({
        //     pageNum:1,
        //     fuJinData:[]
        // })
        console.log('onPullDownRefresh')
        wx.showLoading({
            title: '玩命加载中',
        })
        that.setData({
            pagenum:that.data.pagenum+1
        })
        this.getDataList();
    },

    navbarTap: function(e){
        this.setData({
            currentTab: e.currentTarget.dataset.idx ,
            pagenum:1,
            cutype:parseInt(e.currentTarget.dataset.idx) + 1,
            cufrom:parseInt(e.currentTarget.dataset.idx) + 2,
            list:[],
        })
        this.getDataList();
    },
    getDataList:function(e){
        var that = this;
        wx.hideLoading();
        wx.request({
            url: app.data.server+'/index.php/Tongcheng/Book/getSheQuListBySearch?my=1&pagenum='+that.data.pagenum+"&type="+that.data.cutype+"&uid="+app.globalData.userInfo.uid,
            success:function(d){
                // console.log('123'+JSON.stringify(d.data));
                if(0 != d.data){
                    that.setData({
                        list:that.data.list.concat(d.data)
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var cutype = parseInt(that.data.currentTab)+1;
        var cufrom = parseInt(that.data.currentTab)+2;
        console.log(options)
        this.setData({
            cutype:cutype,
            cufrom:cufrom
        })
        this.getDataList();
    },

    hidePingLun:function(){
        this.setData({
            pingLunData:{isShowPingLun:0}
        })
    },


    showPingLun:function(e){
        var that = this;
        this.setData({
            pingLunData:{isShowPingLun:1},
            pingLunContent:'',
            id:e.currentTarget.dataset.id,
            from:e.currentTarget.dataset.from
        })
    },

    pingluninput:function(e){
        var that = this;
        this.setData({
            pingLunContent:e.detail.value
        })
    },
    fasong:function(e){
        getApp().globalData.iswangchengpinglun=0;
        var that = this;
        var formdata = {
            pingLunContent:this.data.pingLunContent,
            uid:getApp().globalData.userInfo.uid,
            otherid:that.data.id,
            from:that.data.from
        }

        var i = setInterval(function(e){
            // console.log(getApp().globalData.iswangchengpinglun);
            if(1 == getApp().globalData.iswangchengpinglun){
                that.hidePingLun();
                that.setData({
                    list:[
                    ]
                })
                that.getDataList();
                clearInterval(i);
            }
        },1000)
        // console.log(JSON.stringify(formdata))
        app.pinglun(formdata);

    },//点赞
    dianzan:function(e){
        var that = this;
        var from = e.currentTarget.dataset.from;
        var id = e.currentTarget.dataset.id;
        var formdata = {
            otherid:id,
            from:from
        }
        app.dianZan(formdata);
        //修改当前数据源，避免重新请求数据
        //3社区的书评 4社区的话题 5社区的问答 6社区的写文
        var temp = that.data.list;
        // console.log(temp);
        for(var i=0;i<temp.length;i++){
            var item = temp[i];
            if(id == item.id){
                if(1 == item.isDianZan){
                    item.dianzan_num=--item.dianzan_num;
                    item.isDianZan=0;
                }else if(0 == item.isDianZan){
                    item.dianzan_num=++item.dianzan_num;
                    item.isDianZan=1;
                }
            }
        }
        that.setData({
            list:temp
        })
    },
    onShareAppMessage: function (res) {
        // if (res.from === 'button') {
        //   // 来自页面内转发按钮
        //   console.log(res.target)
        // }
        var id = res.target.dataset.id;
        var path = '/pages/community/xiewen/info?id='+id;
        return {
            title: '分享',
            path: path
        }
    },
    delshequ:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.request({
            url:app.data.server+'/index.php/Tongcheng/Book/delSheQu?id='+id,
            success:res=>{
                if(0 == res.data){
                    wx.showToast({
                        title:'删除失败',
                        icon: 'success',
                        duration: 2000
                    })
                }else{
                    wx.showToast({
                        title:'删除成功',
                        icon: 'success',
                        duration: 2000
                    })
                }
                setTimeout(function(){
                    that.setData({
                        list:[],
                        pagenum:1
                    })
                    that.getDataList();
                },1000)

            }
        })
    },
    toShuPingInfo:function(e){
        var id = e.currentTarget.dataset.id;
        console.log('hot'+id)
        wx.navigateTo({
            url:'/pages/community/shuping/info?id='+id
        })
    }
    ,
    toHuaTiInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/huati/info?id='+id
        })
    }
    ,
    toWenDaInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/wenda/info?id='+id
        })
    }
    ,
    toXieWenInfo:function(e){
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url:'/pages/community/xiewen/info?id='+id
        })
    },
})